package org.zpmis.api.business;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.domain.project.qo.StatisticsQo;
import org.zpmis.domain.project.service.ProjectService;
import org.zpmis.domain.project.service.ProjectTaskService;
import org.zpmis.domain.project.vo.ProjectStatisticsVo;
import org.zpmis.domain.user.model.Employee;
import org.zpmis.domain.user.model.SysUser;
import org.zpmis.domain.user.service.EmployeeService;
import org.zpmis.domain.user.service.SysUserService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class StatisticsWebService extends BaseWebService {

    @Reference
    private ProjectService projectService;

    @Reference
    private ProjectTaskService projectTaskService;

    @Reference
    private SysUserService sysUserService;

    @Reference
    private EmployeeService employeeService;

    public List<List<String>> projectUserHoursStatistics(StatisticsQo statisticsQo) {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("人员","工时"));
        List<ProjectStatisticsVo> statisticsVoList = projectService.getProjectUserHoursStatistics(statisticsQo.getId());
        if(statisticsVoList != null && !statisticsVoList.isEmpty()){
            statisticsVoList.stream().forEach(projectStatisticsVo -> {
                Long sysUid = projectStatisticsVo.getSysUid();
                Employee employee = employeeService.findBy("sysUid", sysUid);
                if(employee != null){
                    resultList.add(Arrays.asList(employee.getName(),projectStatisticsVo.getTotalHours()));
                }else {
                    SysUser sysUser = sysUserService.findById(sysUid);
                    if(sysUser != null){
                        resultList.add(Arrays.asList(sysUser.getUserName(),projectStatisticsVo.getTotalHours()));
                    }
                }
            });
        }
        return resultList;
    }
}
